/*******************************************************************************
* Copyright (c) 2010, 2018 西安秦晔信息科技有限公司
* Licensed under the Apache License, Version 2.0 (the "License");
*    you may not use this file except in compliance with the License.
*    You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*    Unless required by applicable law or agreed to in writing, software
*    distributed under the License is distributed on an "AS IS" BASIS,
*    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*    See the License for the specific language governing permissions and
*    limitations under the License.
*******************************************************************************/
package com.qinyeit.serviceapp.service.auxiliary.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.qinyeit.serviceapp.entity.Branch;
import com.qinyeit.serviceapp.entity.CommentBranch;
import com.qinyeit.serviceapp.entity.enums.CommentType;
import com.qinyeit.serviceapp.exception.BusinessRunTimeException;
import com.qinyeit.serviceapp.repository.auxiliary.CommentBranchRepository;
import com.qinyeit.serviceapp.service.branchs.BranchDataReleasedServiceImpl;
import com.qinyeit.serviceapp.service.organization.BranchService;
import com.qinyeit.serviceapp.utils.LocalUtils;
import com.qinyeit.serviceapp.utils.SearchUtils;
import com.qinyetech.springstage.core.date.DateUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.qinyetech.springstage.core.service.BaseService;
import com.qinyeit.serviceapp.entity.Comment;
import com.qinyeit.serviceapp.repository.auxiliary.CommentRepository;
import com.qinyeit.serviceapp.service.auxiliary.CommentService;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/** 
 * ClassName: CommentServiceImpl <br/> 
 * Function: 原因备注 业务实现 <br/> 
 * date: 2018-11-27 10:01:12 <br/> 
 * 
 * @author liuwenjin
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@Transactional
public class CommentServiceImpl extends BaseService<Comment, Long> implements CommentService {


	@Autowired
	private CommentRepository commentRepository;
	@Autowired
	private CommentBranchRepository commentBranchRepository;
	@Autowired
	private BranchService branchService;
	@Autowired
	private BranchDataReleasedServiceImpl branchDataReleasedService;


	@Override
	public void saveData(JSONObject data, Long merchantGroupId, String operator) {
		JSONArray nameData = data.getJSONArray("names");
		String type = data.getString("type");
		if(CollectionUtils.isEmpty(nameData)){
			throw new BusinessRunTimeException("请输入备注名称",10001);
		}
		if(StringUtils.isBlank(type)){
			throw new BusinessRunTimeException("备注类型为空",10002);
		}
		Comment comment = null;
		List<Comment> commentList = Lists.newArrayList();
		for (int i = 0; i < nameData.size(); i++) {
			comment = new Comment();
			comment.setMerchantGroupId(merchantGroupId);
			comment.setName(nameData.getString(i));
			comment.setOperateTime(new Date());
			comment.setOperatorName(operator);
			comment.setType(CommentType.valueOf(type));
			comment.setUuid(LocalUtils.getUUID());
			comment.setUpdateTimestamp(DateUtils.current());
			comment.setSys(false);
			commentList.add(comment);
			comment = this.save(comment);
			if(this.exists(SearchUtils.buildExists(comment.getId(),"name",comment.getName(),merchantGroupId))){
				throw new BusinessRunTimeException("名称不能重复",10001);
			}
		}

	}

	@Override
	public void allocationBranchSave(JSONObject data) {
		JSONArray ids = data.getJSONArray("ids");
		JSONArray branchIds = data.getJSONArray("branchs");
		if(CollectionUtils.isEmpty(ids)){
			throw new BusinessRunTimeException("请选择备注原因",10001);
		}

		if(CollectionUtils.isEmpty(branchIds)){
			throw new BusinessRunTimeException("分店不能为空",10003);
		}
		Comment comment = null;
		Branch branch = null;
		CommentBranch commentBranch = null;
		List<CommentBranch> commentBranchList = Lists.newArrayList();
		List<Comment> commentList = Lists.newArrayList();
		for (int i = 0; i < ids.size(); i++) {
			comment = this.findById(ids.getLong(i));
			if(comment==null){
				throw new BusinessRunTimeException("选择的原因不存在",10004);
			}
			commentList.add(comment);
			//先删除以前的分店信息
			commentRepository.deleteCommenBranch(comment.getId());

			for (int n = 0;n < branchIds.size(); n++) {
				branch = branchService.findById(branchIds.getLong(n));
				if(branch==null){
					throw new BusinessRunTimeException("门店不存在",10005);
				}
				commentBranch = new CommentBranch();
				commentBranch.setBranchId(branchIds.getLong(n));
				commentBranch.setCommentId(comment.getId());
				commentBranch.setDistrictId(branch.getDistrictId());
				commentBranch.setMerchantGroupId(comment.getMerchantGroupId());
				commentBranchList.add(commentBranch);
			}
		}
		commentBranchRepository.saveAll(commentBranchList);
		//数据下发
//		List<Long> dIDs = ids.toJavaList(Long.class);
		List<Long> bIDs = branchIds.toJavaList(Long.class);
		branchDataReleasedService.updateComment(bIDs,commentList);
	}

	@Override
	public void deletedDetails(Long id) {
		commentRepository.deleteCommenBranch(id);
	}

	@Override
	public void deletedBranchCommentData(Comment comment) {
		branchDataReleasedService.deletedBranchCommentData(comment);
	}
}